﻿using NationalInstruments.DAQmx;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using Serilog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Controls;
using 低频天线阵列相控阵信号发生系统SmartFASK12.Models;
using Log = Serilog.Log;
using Task = NationalInstruments.DAQmx.Task;

namespace 低频天线阵列相控阵信号发生系统SmartFASK12
{
    public partial class Form2 : Form
    {
        private Task myTask;
        public Form2()
        {
            InitializeComponent();

            physicalChannelComboBox1.Items.AddRange(MNI.Singleton().GetChannals(PhysicalChannelTypes.AO));
            if (physicalChannelComboBox1.Items.Count > 0)
                physicalChannelComboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// 输出电压
        /// </summary>
        /// <returns></returns>
        public string OutputOoltage(double[,] data)
        {
            // 信号数量
            int signalnumber = 1000;
            // 采样率
            int samplingrate_modulate = 1000;
            // 信号数量
            string physicalChannel = "cDAQ1Mod1/ao0,cDAQ1Mod1/ao1,cDAQ1Mod1/ao2,cDAQ1Mod1/ao3," +
                                     "cDAQ1Mod2/ao0,cDAQ1Mod2/ao1,cDAQ1Mod2/ao2,cDAQ1Mod2/ao3," +
                                     "cDAQ1Mod3/ao0,cDAQ1Mod3/ao1,cDAQ1Mod3/ao2,cDAQ1Mod3/ao3," +
                                     "cDAQ1Mod4/ao0,cDAQ1Mod4/ao1,cDAQ1Mod4/ao2,cDAQ1Mod4/ao3";
            Cursor.Current = Cursors.WaitCursor;
            try
            {
                // Create the task and channel
                myTask = new Task();
                myTask.AOChannels.CreateVoltageChannel(physicalChannel,
                    "",
                    -10,
                    10,
                    AOVoltageUnits.Volts);

                // Verify the task before doing the waveform calculations
                myTask.Control(TaskAction.Verify);
                // Configure the sample clock with the calculated rate and 

                // specified clock source
                myTask.Timing.ConfigureSampleClock(System.String.Empty,//clockSourceTextBox.Text 
                                                                       //fGen.ResultingSampleClockRate,
                    samplingrate_modulate,
                    SampleClockActiveEdge.Rising,
                    SampleQuantityMode.ContinuousSamples, signalnumber);


                myTask.Done += new TaskDoneEventHandler(myTask_Done);

                AnalogMultiChannelWriter writer = new AnalogMultiChannelWriter(myTask.Stream);

                // 将数据写入缓冲区
                writer.WriteMultiSample(false, data);

                // Start writing data
                myTask.Start();

                // Update UI
            }
            catch (DaqException ex)
            {
                Log.Error(ex, ex.Message);
                myTask.Dispose();
                return ex.Message;
            }

            Cursor.Current = Cursors.Default;
            return null;
        }

        /// <summary>
        /// 电压输出完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void myTask_Done(object sender, TaskDoneEventArgs e)
        {
            if (myTask != null)
            {
                myTask.Stop();
                myTask.Dispose();
            }
        }

        /// <summary>
        /// 输出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Start_Click(object sender, EventArgs e)
        {
            double[,] data = new double[16, 1000];
            for (int i = 0; i < 1000; i++)
            {
                data[0, i] = 1;
                data[1, i] = 1.5;
                data[2, i] = 2;
                data[3, i] = 2.5;
                data[4, i] = 3;
                data[5, i] = 3.5;
                data[6, i] = 4;
                data[7, i] = 4.5;
                data[8, i] = 5;
                data[9, i] = 5.5;
                data[10, i] = 6;
                data[11, i] = 6.5;
                data[12, i] = 7;
                data[13, i] = 7.5;
                data[14, i] = 8;
                data[15, i] = 8.5;
            }

            
            OutputOoltage(data);
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Stop_Click(object sender, EventArgs e)
        {
            if (myTask != null)
            {
                myTask.Stop();
                myTask.Dispose();
            }
        }

    }
}
